package com.asynchfileupload.view;

/**  
* FileUploadTag.java - This class crates the file upload HTML and uses the AsynchFileUploadServlet to upload the files.  
* @author  Avinash Singh (349933)
* @version 1.0 
* @see AsynchFileUploadServlet
*/

import java.io.*;
import java.util.*;
 
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.TagSupport;

public class FileUploadTag extends BodyTagSupport {
	
    private String defaultText = null;
    private String defaultTextCSS = "";
    private String uploadButtonText = "Upload";
    private String uploadButtonCSS = "";
    private String uploadHandler= "servlet/FileUploadServlet";
    private String groupName = "file";
    private int progressBarSize = 450;
    
    private int maxSize = -1;
    private int progressFrequency=500;
    
    private String dataHandler;
    
    private Map<String,String> inputParams;
    
    @Override
    public void doInitBody() throws JspException {
    	// TODO Auto-generated method stub
    	super.doInitBody();
    	   System.out.println("FileUploadTag.java :creating UploadTag");     
           
           System.out.println("FileUploadTag.java :groupName " + groupName);        	
           System.out.println("FileUploadTag.java :dataHandler " + dataHandler);
           System.out.println("FileUploadTag.java :uploadHandler " + uploadHandler);
           System.out.println("FileUploadTag.java :progressBarSize " + progressBarSize);
           System.out.println("FileUploadTag.java :progressFrequency " + progressFrequency);
           System.out.println("FileUploadTag.java :defaultText " + defaultText);
           System.out.println("FileUploadTag.java :uploadButtonText " + uploadButtonText);
        
    }
    
    @Override
    public int doAfterBody() throws JspException {
 
        try {
        
        	BodyContent tagbody = this.getBodyContent();
            String bodyContent = tagbody.getString();
            
            JspWriter out = tagbody.getEnclosingWriter();
            
            String createSessionURL = uploadHandler + "?createSession=true" + "&groupName=" + groupName ;
            
            out.println("<style>");
            out.println(".iFrameElementCSS{width:0;height:0;border:0px solid #fff;}");
            out.println(".addFilesButton {cursor:pointer;position:absolute;z-index:1}");
            out.println(".UploadButtonCSS .hiddenFileElement {cursor:pointer;position: relative;text-align: right;-moz-opacity:0;filter:alpha(opacity: 0);opacity: 0;z-index: 2;}");
            out.println("</style>");
            
        	if(dataHandler!=null)
        		createSessionURL += "&dataHandler=" + dataHandler;
        	
        	if(maxSize!=-1)
        		createSessionURL += "&maxSize=" + maxSize;
        	
        	out.println("<span class=\"HiddenIFrameDivCSS\" id=\"HiddenIFrameDiv_" + groupName + "\"/>");
        	
        	out.println("<div class=\"Uploaddiv\" name=\"Uploaddiv\" id=\"Uploaddiv_" + groupName + "\">");
        	
        	out.println("<span class=\"progressBarHolder\" id=\"progressBarHolder_" + groupName + "\"/>");
        	
        	out.println("<iframe id=\"create_session\" src=" + createSessionURL +  " style=\"width:0;height:0;border:0;\"></iframe>");
            
            out.println("<script>uploadHandler='" + uploadHandler + "';");
            out.println("progressFrequency='" + progressFrequency + "';");
            out.println("progressBarMaxSize='" + progressBarSize + "';");
            out.println("</script>");
                        
            if(!(defaultText==null || defaultText.equals("")))
            	out.println("<p>" + defaultText + "</p>");
            
         //   out.println("<iframe id=\"upload_target\" name=\"upload_target\" src=\"#\" style=\"width:0;height:0;border:0px solid #fff;\"></iframe>");
            
            out.println("<div id=\"UploadButton_" + groupName + "\" class=\"UploadButtonCSS\">");
            out.println("<span class=\"addFilesButton\" style=\"vertical-align:middle\">" + uploadButtonText + "</span>");
            out.println("<form name=\"fileform_" + groupName + "\" id=\"fileform_" + groupName + "\" action=\"" + uploadHandler + "\" method=\"post\" enctype=\"multipart/form-data\" target=\"upload_target\" >");
            out.println("<input onchange=\"javascript:sendFileAsynch('" + groupName + "','" + uploadHandler + "');\" class=\"hiddenFileElement\" id=\"datafile_" + groupName + "\" type=\"file\" name=\"datafile\" />");
            out.println(bodyContent);
            out.println("</form>");
            out.println("</div>");
            
            out.println("<span id=\"RemoveButton_" + groupName + "\" class=\"RemoveButtonCSS\">");
            out.println("</span>");
            
          //  InputStream is = this.getClass().getResourceAsStream(jsPath);
            out.println("<script class=\"jsbin\" src=\"servlet/FileUpload/getResource/asyncfileupload.js\"></script>");
            out.println("<link rel=\"stylesheet\" href=\"servlet/FileUpload/getResource/asyncfileupload.css\" type=\"text/css\"/>");
            
            out.println("</div>");
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        return SKIP_BODY;
    }


    public String getDefaultText() {
		return defaultText;
	}

	public void setDefaultText(String defaultText) {
		this.defaultText = defaultText;
	}

	public String getDefaultTextCSS() {
		return defaultTextCSS;
	}

	public void setDefaultTextCSS(String defaultTextCSS) {
		this.defaultTextCSS = defaultTextCSS;
	}

	public String getUploadButtonText() {
		return uploadButtonText;
	}

	public void setUploadButtonText(String uploadButtonText) {
		this.uploadButtonText = uploadButtonText;
	}

	public String getUploadButtonCSS() {
		return uploadButtonCSS;
	}

	public void setUploadButtonCSS(String uploadButtonCSS) {
		this.uploadButtonCSS = uploadButtonCSS;
	}

	public Map<String, String> getInputParams() {
		return inputParams;
	}

	public void setInputParams(Map<String, String> inputParams) {
		this.inputParams = inputParams;
	}

	public String getUploadHandler() {
		return uploadHandler;
	}

	public void setUploadHandler(String uploadHandler) {
		this.uploadHandler = uploadHandler;
	}

	public String getGroupName() {
		return groupName;
	}

	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}


	public int getProgressFrequency() {
		return progressFrequency;
	}


	public void setProgressFrequency(int progressFrequency) {
		this.progressFrequency = progressFrequency;
	}

	public int getMaxSize() {
		return maxSize;
	}


	public void setMaxSize(int maxSize) {
		this.maxSize = maxSize;
	}


	public String getDataHandler() {
		return dataHandler;
	}


	public void setDataHandler(String dataHandler) {
		this.dataHandler = dataHandler;
	}


	public int getProgressBarSize() {
		return progressBarSize;
	}


	public void setProgressBarSize(int progressBarSize) {
		this.progressBarSize = progressBarSize;
	}
}